package cn.edu.zafu.bigdata.mahuilin;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class StudentInfoSystem {
    private static final Map<Integer, Student> studentMap = new HashMap<>();
    private static int studentIdCounter = 1;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addStudent(scanner);
                    break;
                case 2:
                    retrieveStudent(scanner);
                    break;
                case 3:
                    updateStudent(scanner);
                    break;
                case 4:
                    deleteStudent(scanner);
                    break;
                case 5:
                    System.out.println("退出程序。");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效的选项，请重新输入。");
            }
        }
    }

    private static void addStudent(Scanner scanner) {
        System.out.print("请输入学生姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入学生年龄: ");
        int age = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        Student student = new Student(studentIdCounter++, name, age);
        studentMap.put(student.getId(), student);
        System.out.println("学生信息添加成功。");
    }

    private static void retrieveStudent(Scanner scanner) {
        System.out.print("请输入学生ID: ");
        int id = scanner.nextInt();
        Student student = studentMap.get(id);
        if (student != null) {
            System.out.println("学生信息：ID=" + student.getId() + ", 姓名=" + student.getName() + ", 年龄=" + student.getAge());
        } else {
            System.out.println("未找到该学生信息。");
        }
    }

    private static void updateStudent(Scanner scanner) {
        System.out.print("请输入学生ID: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符
        Student student = studentMap.get(id);
        if (student != null) {
            System.out.print("请输入新的学生姓名: ");
            String name = scanner.nextLine();
            System.out.print("请输入新的学生年龄: ");
            int age = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            student.setName(name);
            student.setAge(age);
            System.out.println("学生信息更新成功。");
        } else {
            System.out.println("未找到该学生信息。");
        }
    }

    private static void deleteStudent(Scanner scanner) {
        System.out.print("请输入学生ID: ");
        int id = scanner.nextInt();
        Student student = studentMap.remove(id);
        if (student != null) {
            System.out.println("学生信息删除成功。");
        } else {
            System.out.println("未找到该学生信息。");
        }
    }

    static class Student {
        private int id;
        private String name;
        private int age;

        public Student(int id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}

